package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import com.example.myapplication.moudule.home.fraction.HomeFraction;
import com.example.myapplication.moudule.mine.fraction.MineFration;
import com.example.myapplication.utils.ElementUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.ability.fraction.FractionScheduler;
import ohos.aafwk.content.Intent;
import ohos.agp.components.RadioContainer;

import java.util.ArrayList;
import java.util.List;

/**
 * The MainAbilitySlice, the first page of app
 */
public class MainAbilitySlice extends AbilitySlice {


    private List<Fraction> fractions;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);
        this.getWindow().setStatusBarColor(ElementUtil.getColor(this, ResourceTable.Color_colorSubBackground));
        this.getWindow().setNavigationBarColor(ElementUtil.getColor(this, ResourceTable.Color_colorSubBackground));


        initView();

    }

    private void initFractions() {
        fractions = new ArrayList<>();
        fractions.add(new HomeFraction());
        fractions.add(new MineFration());

        FractionAbility ability = (FractionAbility) getAbility();
        FractionManager fractionManager = ability.getFractionManager();
        FractionScheduler fractionScheduler = fractionManager.startFractionScheduler();
        fractionScheduler.add(ResourceTable.Id_dl, fractions.get(0)).hide(fractions.get(0)).add(ResourceTable.Id_dl, fractions.get(1)).hide(fractions.get(1)).show(fractions.get(0));
        fractionScheduler.submit();

    }

    private void switchFraction(int i) {
        FractionAbility ability = (FractionAbility) getAbility();
        FractionManager fractionManager = ability.getFractionManager();
        FractionScheduler fractionScheduler = fractionManager.startFractionScheduler();

        for (int j = 0; j < fractions.size(); j++) {
            if (j == i) {
                fractionScheduler.show(fractions.get(j));
            } else {
                fractionScheduler.hide(fractions.get(j));
            }
        }
        fractionScheduler.submit();
    }

    /**
     * The initView, get component from xml
     */
    private void initView() {

        RadioContainer componentById = (RadioContainer) findComponentById(ResourceTable.Id_rc);

        initFractions();
        componentById.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(RadioContainer radioContainer, int i) {
                switchFraction(i);
            }
        });
    }



}
